home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.7 / tcpdump- / tcpdump-richard-1.7 / tcpdump-3.0 / print-ether.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-09  |  5.0 KB  |  193 lines

  1. /*
  2.  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21. #ifndef lint
  22. static char rcsid[] =
  23.     "@(#) $Header: print-ether.c,v 1.37 94/06/10 17:01:29 mccanne Exp $ (LBL)";
  24. #endif
  25.  
  26. #include <sys/param.h>
  27. #include <sys/time.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30.  
  31. #include <net/if.h>
  32.  
  33. #include <netinet/in.h>
  34. #include <netinet/if_ether.h>
  35. #include <netinet/in_systm.h>
  36. #include <netinet/ip.h>
  37. #include <netinet/ip_var.h>
  38. #include <netinet/udp.h>
  39. #include <netinet/udp_var.h>
  40. #include <netinet/tcp.h>
  41. #include <netinet/tcpip.h>
  42.  
  43. #include <stdio.h>
  44. #include <pcap.h>
  45.  
  46. #include "interface.h"
  47. #include "addrtoname.h"
  48. #include "ethertype.h"
  49.  
  50. const u_char *packetp;
  51. const u_char *snapend;
  52.  
  53. static inline void
  54. ether_print(register const u_char *bp, int length)
  55. {
  56.     register const struct ether_header *ep;
  57.  
  58.     ep = (const struct ether_header *)bp;
  59.     if (qflag)
  60.         (void)printf("%s %s %d: ",
  61.                  etheraddr_string(ESRC(ep)),
  62.                  etheraddr_string(EDST(ep)),
  63.                  length);
  64.     else
  65.         (void)printf("%s %s %s %d: ",
  66.                  etheraddr_string(ESRC(ep)),
  67.                  etheraddr_string(EDST(ep)),
  68.                  etherproto_string(ep->ether_type),
  69.                  length);
  70. }
  71.  
  72. /*
  73.  * This is the top level routine of the printer.  'p' is the points
  74.  * to the ether header of the packet, 'tvp' is the timestamp,
  75.  * 'length' is the length of the packet off the wire, and 'caplen'
  76.  * is the number of bytes actually captured.
  77.  */
  78. void
  79. ether_if_print(u_char *user, const struct pcap_hdr *h, const u_char *p)
  80. {
  81.     int caplen = h->caplen;
  82.     int length = h->len;
  83.     struct ether_header *ep;
  84.     u_short ether_type;
  85.     extern u_short extracted_ethertype;
  86.  
  87.     ts_print(&h->ts);
  88.  
  89.     if (caplen < sizeof(struct ether_header)) {
  90.         printf("[|ether]");
  91.         goto out;
  92.     }
  93.  
  94.     if (eflag)
  95.         ether_print(p, length);
  96.  
  97.     /*
  98.      * Some printers want to get back at the ethernet addresses,
  99.      * and/or check that they're not walking off the end of the packet.
  100.      * Rather than pass them all the way down, we set these globals.
  101.      */
  102.     packetp = p;
  103.     snapend = p + caplen;
  104.  
  105.     length -= sizeof(struct ether_header);
  106.     caplen -= sizeof(struct ether_header);
  107.     ep = (struct ether_header *)p;
  108.     p += sizeof(struct ether_header);
  109.  
  110.     ether_type = ntohs(ep->ether_type);
  111.  
  112.     /*
  113.      * Is it (gag) an 802.3 encapsulation?
  114.      */
  115.     extracted_ethertype = 0;
  116.     if (ether_type < ETHERMTU) {
  117.         /* Try to print the LLC-layer header & higher layers */
  118.         if (llc_print(p, length, caplen, ESRC(ep), EDST(ep)) == 0) {
  119.             /* ether_type not known, print raw packet */
  120.             if (!eflag)
  121.                 ether_print((u_char *)ep, length);
  122.             if (extracted_ethertype) {
  123.                 printf("(LLC %s) ",
  124.                    etherproto_string(htons(extracted_ethertype)));
  125.             }
  126.             if (!xflag && !qflag)
  127.                 default_print(p, caplen);
  128.         }
  129.     } else if (ether_encap_print(ether_type, p, length, caplen) == 0) {
  130.         /* ether_type not known, print raw packet */
  131.         if (!eflag)
  132.             ether_print((u_char *)ep, length + sizeof(*ep));
  133.         if (!xflag && !qflag)
  134.             default_print(p, caplen);
  135.     }
  136.     if (xflag)
  137.         default_print(p, caplen);
  138.  out:
  139.     putchar('\n');
  140. }
  141.  
  142. /*
  143.  * Prints the packet encapsulated in an Ethernet data segment
  144.  * (or an equivalent encapsulation), given the Ethernet type code.
  145.  *
  146.  * Returns non-zero if it can do so, zero if the ethertype is unknown.
  147.  *
  148.  * Stuffs the ether type into a global for the benefit of lower layers
  149.  * that might want to know what it is.
  150.  */
  151.  
  152. u_short    extracted_ethertype;
  153.  
  154. int
  155. ether_encap_print(u_short ethertype, const u_char *p, int length, int caplen)
  156. {
  157.     extracted_ethertype = ethertype;
  158.  
  159.     switch (ethertype) {
  160.  
  161.     case ETHERTYPE_IP:
  162.         ip_print(p, length);
  163.         return (1);
  164.  
  165.     case ETHERTYPE_ARP:
  166.     case ETHERTYPE_REVARP:
  167.         arp_print(p, length, caplen);
  168.         return (1);
  169.  
  170.     case ETHERTYPE_DN:
  171.         decnet_print(p, length, caplen);
  172.         return (1);
  173.  
  174.     case ETHERTYPE_ATALK:
  175.         if (vflag)
  176.             fputs("et1 ", stdout);
  177.         atalk_print(p, length);
  178.         return (1);
  179.  
  180.     case ETHERTYPE_AARP:
  181.         aarp_print(p, length);
  182.         return (1);
  183.  
  184.     case ETHERTYPE_LAT:
  185.     case ETHERTYPE_MOPRC:
  186.     case ETHERTYPE_MOPDL:
  187.         /* default_print for now */
  188.     default:
  189.         return (0);
  190.     }
  191. }
  192.  
  193.